import factory.Component;
import factory.RequestMapping;
import factory.Resource;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Test {
    public static void main(String[] args) throws Exception {
        //获取类上面的注解
        //  1. 获取Class对象
//        Class clz = Class.forName("factory.Apple");
//        if(clz.isAnnotationPresent(Component.class)){//判断类上面有没有写Component注解
//            //获取注解对象
//            Component component = (Component)clz.getAnnotation(Component.class);
//            //调用注解对象里面方法
//            String value = component.value();
//            System.out.println(value);
//        }
        Class clz = Class.forName("factory.Apple");
        if(clz.isAnnotationPresent(Component.class)){
            Component component = (Component) clz.getAnnotation(Component.class);
            String value = component.value();
            System.out.println(value);
        }

        //获取属性上面的注解
        //1. 获取属性对象
        //获取Class对象
//        Class clz2 = Class.forName("factory.Apple");
//        Field brand = clz2.getDeclaredField("brand");
//
//        if(brand.isAnnotationPresent(Resource.class)){//判断属性上面有没有写Resource注解
//            //获取注解对象
//           Resource resource= (Resource)brand.getAnnotation(Resource.class);
//           //调用注解的方法
//            String name = resource.name();
//            System.out.println(name);
//        }
        Class clz2 = Class.forName("factory.Apple");
        Field brand = clz2.getDeclaredField("brand");
        if(brand.isAnnotationPresent(Resource.class)){
            Resource resource = (Resource) brand.getAnnotation(Resource.class);
            String name = resource.name();
            System.out.println(name);
        }
        //获取方法上面的注解
        //1.获取方法对象
        //获取Class对象
//        Class clz3 = Class.forName("factory.Apple");
//        Method getBrand = clz3.getDeclaredMethod("getBrand");
//
//        if(getBrand.isAnnotationPresent(RequestMapping.class)){//判断方法上面有没有写RequestMapping注解
//            //获取注解对象
//            RequestMapping requestMapping=(RequestMapping)getBrand.getAnnotation(RequestMapping.class);
//            //调用方法
//            String value = requestMapping.value();
//            String method = requestMapping.method();
//            System.out.println(value+","+method);
//        }
        Class clz3 = Class.forName("factory.Apple");
        Method getBrand = clz3.getDeclaredMethod("getBrand");
        if(getBrand.isAnnotationPresent(RequestMapping.class)){
            RequestMapping requestMapping =(RequestMapping)getBrand.getAnnotation(RequestMapping.class);
            String value = requestMapping.value();
            String method = requestMapping.method();
            System.out.println(value+","+method);
        }
    }
}
